package com.jyniubi.core.controller.api;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jyniubi.base.result.R;
import com.jyniubi.base.utils.JwtUtils;
import com.jyniubi.core.pojo.entity.IntegralGrade;
import com.jyniubi.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author jyzhang
 * @since 2024-04-27
 */
@Api(tags = "积分等级会员模块")
@RestController
@RequestMapping("/api/core/integralGrade")
public class ApiIntegralGradeController {
    @Resource
    IntegralGradeService integralGradeService;

    /**
     * 查询所有积分等级
     * @return
     */
    @ApiOperation(value = "查询所有积分等级")
    @GetMapping
    public List<IntegralGrade> list(){
        return integralGradeService.list();
    }

    /**
     * 根据id查询对应的积分等级数据
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询对应的积分等级数据")
    @GetMapping("/{id}")
    public IntegralGrade getById(@PathVariable("id")String id){
        return integralGradeService.getById(id);
    }

    /**
     * 根据id删除对应的积分等级数据
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除对应的积分等级数据")
    @DeleteMapping("/{id}")
    public Boolean deleteById(@PathVariable("id")String id){
        return integralGradeService.removeById(id);
    }

    /**
     *  分页
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "积分等级分页查询")
    @GetMapping("/list/{pageNum}/{pageSize}")
    public Page<IntegralGrade> pageList(@PathVariable("pageNum") Integer pageNum,
                                        @PathVariable("pageSize") Integer pageSize){
        return integralGradeService.page(new Page<>(pageNum,pageSize));
    }

    /**
     * 新增
     * @param integralGrade
     * @return
     */
    @ApiOperation(value = "积分等级新增")
    @PostMapping
    public Boolean save(@RequestBody IntegralGrade integralGrade){
        return integralGradeService.save(integralGrade);
    }

    @ApiOperation("查询会员的积分等级借款额度")
    @GetMapping("/auth/borrowAmount")
    public R borrowAmount(@RequestHeader(value = "token",required = false) String token){
        Long userId = JwtUtils.getUserId(token);
        return R.ok().data("borrowAmount",integralGradeService.borrowAmount(userId));
    }

}

